﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows.Controls;
using System.Windows;
using System.Collections;

namespace Luna.WPF.Behaviors
{
    public class ListBoxSwapBehavior : Behavior<ListBox>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
        }



        public ListBox SwapListBox
        {
            get { return (ListBox)GetValue(SwapListBoxProperty); }
            set { SetValue(SwapListBoxProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SwapListBox.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SwapListBoxProperty =
            DependencyProperty.Register("SwapListBox", typeof(ListBox), typeof(ListBoxSwapBehavior), 
            new UIPropertyMetadata(null));


    }

    public class ListBoxSwapAction : TargetedTriggerAction<FrameworkElement>
    {
        public ListBox FirstListBox
        {
            get { return (ListBox)GetValue(FirstListBoxProperty); }
            set { SetValue(FirstListBoxProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SwapListBox.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FirstListBoxProperty =
            DependencyProperty.Register("FirstListBox", typeof(ListBox), typeof(ListBoxSwapAction),
            new UIPropertyMetadata(null));



        public ListBox SecondListBox
        {
            get { return (ListBox)GetValue(SecondListBoxProperty); }
            set { SetValue(SecondListBoxProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SecondListBox.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SecondListBoxProperty =
            DependencyProperty.Register("SecondListBox", typeof(ListBox), typeof(ListBoxSwapAction), 
            new UIPropertyMetadata(null));


        protected override void Invoke(object parameter)
        {
            if (FirstListBox.SelectedItem == null && SecondListBox.SelectedItem == null)
                return;
            var x = FirstListBox.ItemsSource as IList;
            var y = SecondListBox.ItemsSource as IList;
            if (FirstListBox.SelectedItem != null)
            {
                var flag = FirstListBox.SelectedItem;
                y.Add(FirstListBox.SelectedItem);
                x.Remove(flag);
                SecondListBox.SelectedItem = flag;
                //SelectedItem = SecondListBox.SelectedItem;
            }
            else
            {
                x.Add(SecondListBox.SelectedItem);
                var flag = SecondListBox.SelectedItem;
                y.Remove(SecondListBox.SelectedItem);
                FirstListBox.SelectedItem = flag;
               // SelectedItem = FirstListBox.SelectedItem;
            }
        }
    }
}
